package com.wanmait.myhouse.dao;
import com.wanmait.myhouse.util.DBHelper;
import com.wanmait.myhouse.vo.City;
import com.wanmait.myhouse.vo.House;
import com.wanmait.myhouse.vo.Room;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class CityDaO {
    public void MgAddCity(City city){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into t_city(city_name) values(?)";
        try {
            queryRunner.update(sql,city.getCityName());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public City findCity(City city){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select * from t_city where city_name=?";
        try {
           City city1 = queryRunner.query(sql, new BeanHandler<>(City.class), city.getCityName());
            return city1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }



    public List<City> FindAll(){//查找所有house
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,city_name cityName from t_city;";
        try {
            List<City>cityList = queryRunner.query(sql,new BeanListHandler<>(City.class));
            return cityList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
